/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package walrus.zip;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

import walrus.base.ArticleBuffer;
import walrus.base.Constants;

public class Archive {
	
	public static void main(String[] args) throws Exception {
		Archive archive = new Archive(new File("article-archive-8.zip"));
		System.out.println(archive.open().articles());
	}

	public static boolean isArchive(String path) {
		try {
			new Archive(new File(path)).open();
			return true;
		} catch (IOException e) {
			// Probably not an archive
		}
		return false;
	}
	
	private static final int DEFAULT_CAPACITY = 128;
	private final File file;
	private ZipFile zip;

	public Archive(File archive) {
		super();
		this.file = archive;
	}
	
	public Archive open() throws IOException {
		zip = new ZipFile(this.file);
		return this;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> titles() {
		final List<String> results = new ArrayList<String>(DEFAULT_CAPACITY);
		for (Enumeration<ZipArchiveEntry> en = zip.getEntries(); en.hasMoreElements();) {
			results.add(en.nextElement().getComment());
		}
		return results;
	}
	
	@SuppressWarnings("unchecked")
	public List<Article> articles() {
		final List<Article> results = new ArrayList<Article>(DEFAULT_CAPACITY);
		for (Enumeration<ZipArchiveEntry> en = zip.getEntries(); en.hasMoreElements();) {
			results.add(new Article(en.nextElement(), zip));
		}
		return results;
	}	
	
	public static class Article {
		private final ZipFile zip;
		private final ZipArchiveEntry entry;
		private final String title;

		public Article(final ZipArchiveEntry entry, final ZipFile zip) {
			super();
			this.entry = entry;
			this.zip = zip;
			title = entry.getComment();
		}
		
		public String getTitle() {
			return title;
		}
		
		public void fill(final ArticleBuffer buffer) throws IOException {
			buffer.reset();
			buffer.setTitle(title);
			
			final Reader in = new InputStreamReader(zip.getInputStream(entry), Constants.UTF_8);
			buffer.read(in);
		}

		@Override
		public String toString() {
			return "Article [title=" + title + "]";
		}
	}
}
